package tests;

import common.AutotestUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.io.IOException;

public class RegisterPage extends AutotestUtils {
    static String url = "http://113.45.242.167:8080/register.html";

    public RegisterPage() {
        super(url);
    }
    // 检查注册功能 -- 成功注册

    public void registerSuccess() throws InterruptedException {
        // 先清空输入框
        driver.findElement(By.cssSelector("#userName > input[type=text]")).clear();

        driver.findElement(By.cssSelector("#password > input[type=password]")).clear();

        driver.findElement(By.cssSelector("#password2 > input[type=password]")).clear();
        driver.findElement(By.cssSelector("#inputCaptcha")).clear();

        // 输入正确用户名+密码+万能验证码
        driver.findElement(By.cssSelector("#userName > input[type=text]")).sendKeys("Autotest");
        driver.findElement(By.cssSelector("#password > input[type=password]")).sendKeys("123456");
        driver.findElement(By.cssSelector("#password2 > input[type=password]")).sendKeys("123456");
        driver.findElement(By.cssSelector("#inputCaptcha")).sendKeys("xieyeqi");

        // 注册
        driver.findElement(By.cssSelector("#submit")).click();

        Thread.sleep(2000);

        // 判断是否注册成功
        Alert alert = driver.switchTo().alert();
        assert "注册成功".equals(alert.getText());
        alert.accept();
        // 退出

    }


    // 检查注册功能 -- 异常注册
    public void registerFail() throws InterruptedException, IOException {

        try {
            // 先清空输入框
            driver.findElement(By.cssSelector("#userName > input[type=text]")).clear();
            driver.findElement(By.cssSelector("#password > input[type=password]")).clear();
            driver.findElement(By.cssSelector("#password2 > input[type=password]")).clear();
            driver.findElement(By.cssSelector("#inputCaptcha")).clear();


            // 输入用户名+密码+不同的密码+万能验证码
            driver.findElement(By.cssSelector("#userName > input[type=text]")).sendKeys("Autotest");
            driver.findElement(By.cssSelector("#password > input[type=password]")).sendKeys("123456");
            driver.findElement(By.cssSelector("#password2 > input[type=password]")).sendKeys("1234567");
            driver.findElement(By.cssSelector("#inputCaptcha")).sendKeys("xieyeqi");
            // 注册
            driver.findElement(By.cssSelector("#submit")).click();

            Thread.sleep(2000);

            // 判断是否注册成功
            Alert alert = driver.switchTo().alert();
            String text = alert.getText();
            System.out.println(text);
            assert text.equals("两次输入的密码不一致");
            alert.accept();
            String methodName = new Object() {
            }.getClass().getEnclosingMethod().getName();
            getTakeScreenShot(getClass().getName() + "." + methodName);
            // 退出
        } finally {
            driver.quit();
        }


    }
}
